var com = new CommonUtil();
$(function() {
    var Order = {
        basePath:com.basePath(),
        valiCal: /^1[3456789]\d{9}$/,
        code: /^\d{6}$/,
        countdown :60 ,//发送短信验证码 到计时
        settime: function(obj){//倒计时
            var that = Order;
            if (that.countdown == 0) {
                obj.attr('disabled', false);
                obj.text("获取验证码").css({"background-color": "#009EFF"});
                that.countdown = 60;
                return;
            } else {
                obj.attr('disabled', true).css({"background-color": "#ccc"});;
                obj.text("重新发送(" + that.countdown + ")");
                that.countdown--;
            }
            setTimeout(function () {
                that.settime(obj)
            }, 1000);
        },
        /**
         * 发起ajax请求
         */
        ajaxRequst:function(url,type,data,func) {
            var mobile = $("[name='mobile']").val();
            var code=$("[name='code']").val();
            var o = this;
            if(!url || !data){
                return;
            }
            $.ajax({
                url:url,
                type: type,
                dataType: "json",
                data: data,
//                async: false,
//                contentType:'application/json;charset=UTF-8',
                success: function(data) {
//                	var data = eval("("+data+")");//转换为json对象
                    if(data.success){
                        if($.isFunction(func)){
                            func($("#butt"));
                        } else {
                            if(url.indexOf("verifyCode")>0){
                                window.location.href = "toMy";
                            }
                        }
                    }
                },
                error: function() {
                    layer.open({content:'数据出现问题,请重试',btn: ['确认']});
                }
            });
//        	return false; // 阻止表单自动提交事件
        },
        bindClick:function () {
            var that = this;
            $(".dl button").click(function () {
                if (!that.code.test($("#code").val())) {
                    layer.open({content:'你输入验证码格式有误！',btn: ['确认']});
                }else{
                    that.ajaxRequst(that.basePath+"/message/verifyCode?","post","code="+$("#code").val()+"&mobile="+$("#cal").val());
                }
            });
            $("#butt").click(function () {
                if (!that.valiCal.test($("#cal").val())) {
                    layer.open({content:'你输入的号码格式有误！',btn: ['确认']});
                } else {
                    $(this).attr('disabled', true);
                    that.ajaxRequst(that.basePath+"/message/send?","post","mobile="+$("#cal").val(),that.settime)
                }
            });
        },
        init:function () {
            this.bindClick();
        }
    };
    Order.init();
});



